import fs from "fs";
import archiver from "archiver";
import { dirname, join } from "path";
import { fileURLToPath } from "url";

const __dirname = dirname(fileURLToPath(import.meta.url));
const distDir = join(__dirname, "dist");
const now = new Date();
const zipFile = join(
  __dirname,
  "/dist-" +
    now.getFullYear() +
    "-" +
    (now.getMonth() + 1).toString().padStart(2, "0") +
    now.getDate().toString().padStart(2, "0") +
    "-" +
    now.getHours().toString().padStart(2, "0") +
    now.getMinutes().toString().padStart(2, "0") +
    ".zip"
);

const output = fs.createWriteStream(zipFile);
output.on("close", () => {
  console.log(`打包完成： ${zipFile}`);
});

const zip = archiver("zip", { zlib: { level: 9 } });
zip.on("error", (err) => console.error(err));
zip.pipe(output);

zip.directory(distDir, false, {});

zip.finalize();
